{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# 模型加载及恢复训练\n",
    "\n",
    "在快速入门中，我们已经介绍了将训练好的模型保存到磁盘文件的方法。应用程序可以随时加载模型，完成预测任务。但是在日常训练工作中我们会遇到一些突发情况，导致训练过程主动或被动的中断。如果训练一个模型需要花费几天的训练时间，中断后从初始状态重新训练是不可接受的。\n",
    "\n",
    "万幸的是，飞桨支持从上一次保存状态开始训练，只要我们随时保存训练过程中的模型状态，就不用从初始状态重新训练。\n",
    "\n",
    "下面介绍恢复训练的代码实现，依然使用手写数字识别的案例，在网络定义的部分保持不变。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "loading mnist dataset from ./work/mnist.json.gz ......\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import random\n",
    "import paddle\n",
    "import paddle.fluid as fluid\n",
    "from paddle.fluid.dygraph.nn import Conv2D, Pool2D, FC\n",
    "import numpy as np\n",
    "from PIL import Image\n",
    "\n",
    "import gzip\n",
    "import json\n",
    "\n",
    "# 定义数据集读取器\n",
    "def load_data(mode='train'):\n",
    "\n",
    "    # 数据文件\n",
    "    datafile = './work/mnist.json.gz'\n",
    "    print('loading mnist dataset from {} ......'.format(datafile))\n",
    "    data = json.load(gzip.open(datafile))\n",
    "    train_set, val_set, eval_set = data\n",
    "\n",
    "    # 数据集相关参数，图片高度IMG_ROWS, 图片宽度IMG_COLS\n",
    "    IMG_ROWS = 28\n",
    "    IMG_COLS = 28\n",
    "\n",
    "    if mode == 'train':\n",
    "        imgs = train_set[0]\n",
    "        labels = train_set[1]\n",
    "    elif mode == 'valid':\n",
    "        imgs = val_set[0]\n",
    "        labels = val_set[1]\n",
    "    elif mode == 'eval':\n",
    "        imgs = eval_set[0]\n",
    "        labels = eval_set[1]\n",
    "\n",
    "    imgs_length = len(imgs)\n",
    "\n",
    "    assert len(imgs) == len(labels), \\\n",
    "          \"length of train_imgs({}) should be the same as train_labels({})\".format(\n",
    "                  len(imgs), len(labels))\n",
    "                  \n",
    "    index_list = list(range(imgs_length))\n",
    "\n",
    "    # 读入数据时用到的batchsize\n",
    "    BATCHSIZE = 100\n",
    "\n",
    "    # 定义数据生成器\n",
    "    def data_generator():\n",
    "        #if mode == 'train':\n",
    "        #    random.shuffle(index_list)\n",
    "        imgs_list = []\n",
    "        labels_list = []\n",
    "        for i in index_list:\n",
    "            img = np.reshape(imgs[i], [1, IMG_ROWS, IMG_COLS]).astype('float32')\n",
    "            label = np.reshape(labels[i], [1]).astype('int64')\n",
    "            imgs_list.append(img) \n",
    "            labels_list.append(label)\n",
    "            if len(imgs_list) == BATCHSIZE:\n",
    "                yield np.array(imgs_list), np.array(labels_list)\n",
    "                imgs_list = []\n",
    "                labels_list = []\n",
    "\n",
    "        # 如果剩余数据的数目小于BATCHSIZE，\n",
    "        # 则剩余数据一起构成一个大小为len(imgs_list)的mini-batch\n",
    "        if len(imgs_list) > 0:\n",
    "            yield np.array(imgs_list), np.array(labels_list)\n",
    "\n",
    "    return data_generator\n",
    "\n",
    "#调用加载数据的函数\n",
    "train_loader = load_data('train')\n",
    "\n",
    "# 定义模型结构\n",
    "class MNIST(fluid.dygraph.Layer):\n",
    "     def __init__(self, name_scope):\n",
    "         super(MNIST, self).__init__(name_scope)\n",
    "         name_scope = self.full_name()\n",
    "         self.conv1 = Conv2D(name_scope, num_filters=20, filter_size=5, stride=1, padding=2, act=\"relu\")\n",
    "         self.pool1 = Pool2D(name_scope, pool_size=2, pool_stride=2, pool_type='max')\n",
    "         self.conv2 = Conv2D(name_scope, num_filters=20, filter_size=5, stride=1, padding=2, act=\"relu\")\n",
    "         self.pool2 = Pool2D(name_scope, pool_size=2, pool_stride=2, pool_type='max')\n",
    "         self.fc = FC(name_scope, size=10, act='softmax')\n",
    "     \n",
    "     #加入分类准确率的评估指标\n",
    "     def forward(self, inputs, label=None):\n",
    "         x = self.conv1(inputs)\n",
    "         x = self.pool1(x)\n",
    "         x = self.conv2(x)\n",
    "         x = self.pool2(x)\n",
    "         x = self.fc(x)\n",
    "         if label is not None:\n",
    "             acc = fluid.layers.accuracy(input=x, label=label)\n",
    "             return x, acc\n",
    "         else:\n",
    "             return x\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "在开始介绍使用飞桨恢复训练前，先正常训练一个模型，优化器使用Adam，使用动态变化的学习率，学习率从0.01衰减到0.001。每训练一轮后保存一次模型，之后将采用其中某一轮的模型参数进行恢复训练，验证一次性训练和中断再恢复训练的模型表现是否一致（训练loss的变化）。\n",
    "\n",
    "注意进行恢复训练的程序不仅要保存模型参数，还要保存优化器参数。这是因为某些优化器含有一些随着训练过程变换的参数，例如Adam, Adagrad等优化器采用可变学习率的策略，随着训练进行会逐渐减少学习率。这些优化器的参数对于恢复训练至关重要。\n",
    "\n",
    "为了演示这个特性,下面训练程序使用adam优化器，学习率以多项式曲线从0.01衰减到0.001（polynomial decay）。\n",
    "``` \n",
    "lr = fluid.dygraph.PolynomialDecay(0.01, total_steps, 0.001)\n",
    "``` \n",
    "* learning_rate：初始学习率\n",
    "* decay_steps：衰减步数\n",
    "* end_learning_rate：最终学习率\n",
    "* power：多项式的幂，默认值为1.0\n",
    "* cycle：下降后是否重新上升，polynomial decay的变化曲线下图所示。\n",
    "\n",
    "<img src=\"https://ai-studio-static-online.cdn.bcebos.com/4877393d22a445098adc002b53ebbbcef82f8b0ac46d409ca7e3a19e2622fe8a\" width=300> \n",
    "<br></br>\t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch: 0, batch: 0, loss is: [2.4541051], acc is [0.12]\n",
      "epoch: 0, batch: 200, loss is: [0.14235404], acc is [0.95]\n",
      "epoch: 0, batch: 400, loss is: [0.04791263], acc is [0.99]\n",
      "epoch: 1, batch: 0, loss is: [0.04537241], acc is [0.98]\n",
      "epoch: 1, batch: 200, loss is: [0.04753707], acc is [0.98]\n",
      "epoch: 1, batch: 400, loss is: [0.02443215], acc is [0.99]\n",
      "epoch: 2, batch: 0, loss is: [0.0282321], acc is [0.99]\n",
      "epoch: 2, batch: 200, loss is: [0.04439911], acc is [0.99]\n",
      "epoch: 2, batch: 400, loss is: [0.03719958], acc is [0.98]\n",
      "epoch: 3, batch: 0, loss is: [0.04289422], acc is [0.99]\n",
      "epoch: 3, batch: 200, loss is: [0.02114591], acc is [0.99]\n",
      "epoch: 3, batch: 400, loss is: [0.00777181], acc is [1.]\n",
      "epoch: 4, batch: 0, loss is: [0.01535593], acc is [0.99]\n",
      "epoch: 4, batch: 200, loss is: [0.00227343], acc is [1.]\n",
      "epoch: 4, batch: 400, loss is: [0.0032079], acc is [1.]\n"
     ]
    }
   ],
   "source": [
    "#在使用GPU机器时，可以将use_gpu变量设置成True\n",
    "use_gpu = False\n",
    "place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace()\n",
    "\n",
    "with fluid.dygraph.guard(place):\n",
    "    model = MNIST(\"mnist\")\n",
    "    model.train() \n",
    "    \n",
    "    EPOCH_NUM = 5\n",
    "    BATCH_SIZE = 100\n",
    "    # 定义学习率，并加载优化器参数到模型中\n",
    "    total_steps = (int(60000//BATCH_SIZE) + 1) * EPOCH_NUM\n",
    "    lr = fluid.dygraph.PolynomialDecay(0.01, total_steps, 0.001)\n",
    "    \n",
    "    # 使用Adam优化器\n",
    "    optimizer = fluid.optimizer.AdamOptimizer(learning_rate=lr)\n",
    "    \n",
    "    for epoch_id in range(EPOCH_NUM):\n",
    "        for batch_id, data in enumerate(train_loader()):\n",
    "            #准备数据，变得更加简洁\n",
    "            image_data, label_data = data\n",
    "            image = fluid.dygraph.to_variable(image_data)\n",
    "            label = fluid.dygraph.to_variable(label_data)\n",
    "            \n",
    "            #前向计算的过程，同时拿到模型输出值和分类准确率\n",
    "            predict, acc = model(image, label)\n",
    "            avg_acc = fluid.layers.mean(acc)\n",
    "            \n",
    "            #计算损失，取一个批次样本损失的平均值\n",
    "            loss = fluid.layers.cross_entropy(predict, label)\n",
    "            avg_loss = fluid.layers.mean(loss)\n",
    "            \n",
    "            #每训练了200批次的数据，打印下当前Loss的情况\n",
    "            if batch_id % 200 == 0:\n",
    "                print(\"epoch: {}, batch: {}, loss is: {}, acc is {}\".format(epoch_id, batch_id, avg_loss.numpy(),avg_acc.numpy()))\n",
    "            \n",
    "            #后向传播，更新参数的过程\n",
    "            avg_loss.backward()\n",
    "            optimizer.minimize(avg_loss)\n",
    "            model.clear_gradients()\n",
    "            \n",
    "        # 保存模型参数和优化器的参数\n",
    "        fluid.save_dygraph(model.state_dict(), './checkpoint/mnist_epoch{}'.format(epoch_id))\n",
    "        fluid.save_dygraph(optimizer.state_dict(), './checkpoint/mnist_epoch{}'.format(epoch_id))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# 恢复训练\n",
    "\n",
    "在上述训练代码中，我们训练了五轮（epoch）。在每轮结束时，我们均保存了模型参数和优化器相关的参数。\n",
    "\n",
    "- 使用``model.state_dict()``获取模型参数。\n",
    "- 使用``optimizer.state_dict()``获取优化器和学习率相关的参数。\n",
    "- 调用``fluid.save_dygraph()``将参数保存到本地。\n",
    "\n",
    "比如第一轮训练保存的文件是mnist_epoch0.pdparams，mnist_epoch0.pdopt，分别存储了模型参数和优化器参数。\n",
    "\n",
    "当加载模型时，如果模型参数文件和优化器参数文件是相同的，我们可以使用``load_dygraph``同时加载这两个文件，如下代码所示。\n",
    "\n",
    "``` \n",
    "params_dict, opt_dict = fluid.load_dygraph(params_path)\n",
    "``` \n",
    "\n",
    "如果模型参数文件和优化器参数文件的名字不同，需要调用两次``load_dygraph``分别获得模型参数和优化器参数。\n",
    "\n",
    "如何判断是模型是否准确的恢复训练呢？\n",
    "\n",
    "理想的恢复训练是模型状态回到训练中断的时刻，恢复训练之后的梯度更新走向是和恢复训练前的梯度走向是完全相同的。基于此，我们可以通过恢复训练后的损失变化，判断上述方法是否能准确的恢复训练。即从epoch 0结束时保存的模型参数和优化器状态恢复训练，校验其后训练的损失变化（epoch 1）是否和不中断时的训练完全一致。\n",
    "\n",
    "------\n",
    "**说明：**\n",
    "\n",
    "恢复训练有如下两个要点：\n",
    "* 保存模型时同时保存模型参数和优化器参数。\n",
    "* 恢复参数时同时恢复模型参数和优化器参数。\n",
    "\n",
    "------\n",
    "\n",
    "下面的代码将展示恢复训练的过程，并验证恢复训练是否成功。其中，我们重新定义一个``train_again()``训练函数，加载模型参数并从第一个epoch开始训练，以便读者可以校验恢复训练后的损失变化。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch: 1, batch: 0, loss is: [0.04537241], acc is [0.98]\n",
      "epoch: 1, batch: 200, loss is: [0.04753707], acc is [0.98]\n",
      "epoch: 1, batch: 400, loss is: [0.02443215], acc is [0.99]\n",
      "epoch: 2, batch: 0, loss is: [0.0282321], acc is [0.99]\n",
      "epoch: 2, batch: 200, loss is: [0.04439911], acc is [0.99]\n",
      "epoch: 2, batch: 400, loss is: [0.03719958], acc is [0.98]\n",
      "epoch: 3, batch: 0, loss is: [0.04289422], acc is [0.99]\n",
      "epoch: 3, batch: 200, loss is: [0.02114591], acc is [0.99]\n",
      "epoch: 3, batch: 400, loss is: [0.00777181], acc is [1.]\n",
      "epoch: 4, batch: 0, loss is: [0.01535593], acc is [0.99]\n",
      "epoch: 4, batch: 200, loss is: [0.00227343], acc is [1.]\n",
      "epoch: 4, batch: 400, loss is: [0.0032079], acc is [1.]\n"
     ]
    }
   ],
   "source": [
    "params_path = \"./checkpoint/mnist_epoch0\"        \n",
    "#在使用GPU机器时，可以将use_gpu变量设置成True\n",
    "use_gpu = False\n",
    "place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace()\n",
    "\n",
    "with fluid.dygraph.guard(place):\n",
    "    # 加载模型参数到模型中\n",
    "    params_dict, opt_dict = fluid.load_dygraph(params_path)\n",
    "    model = MNIST(\"mnist\")\n",
    "    model.load_dict(params_dict)\n",
    "    \n",
    "    EPOCH_NUM = 5\n",
    "    BATCH_SIZE = 100\n",
    "    # 定义学习率，并加载优化器参数到模型中\n",
    "    total_steps = (int(60000//BATCH_SIZE) + 1) * EPOCH_NUM\n",
    "    lr = fluid.dygraph.PolynomialDecay(0.01, total_steps, 0.001)\n",
    "    \n",
    "    # 使用Adam优化器\n",
    "    optimizer = fluid.optimizer.AdamOptimizer(learning_rate=lr)\n",
    "    optimizer.set_dict(opt_dict)\n",
    "\n",
    "    for epoch_id in range(1, EPOCH_NUM):\n",
    "        for batch_id, data in enumerate(train_loader()):\n",
    "            #准备数据，变得更加简洁\n",
    "            image_data, label_data = data\n",
    "            image = fluid.dygraph.to_variable(image_data)\n",
    "            label = fluid.dygraph.to_variable(label_data)\n",
    "            \n",
    "            #前向计算的过程，同时拿到模型输出值和分类准确率\n",
    "            predict, acc = model(image, label)\n",
    "            avg_acc = fluid.layers.mean(acc)\n",
    "            \n",
    "            #计算损失，取一个批次样本损失的平均值\n",
    "            loss = fluid.layers.cross_entropy(predict, label)\n",
    "            avg_loss = fluid.layers.mean(loss)\n",
    "            \n",
    "            #每训练了200批次的数据，打印下当前Loss的情况\n",
    "            if batch_id % 200 == 0:\n",
    "                print(\"epoch: {}, batch: {}, loss is: {}, acc is {}\".format(epoch_id, batch_id, avg_loss.numpy(),avg_acc.numpy()))\n",
    "            \n",
    "            #后向传播，更新参数的过程\n",
    "            avg_loss.backward()\n",
    "            optimizer.minimize(avg_loss)\n",
    "            model.clear_gradients()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "从恢复训练的损失变化来看，加载模型参数继续训练的损失函数值和正常训练损失函数值是完全一致的，可见使用飞桨实现恢复训练是极其简单的。\n",
    "\n",
    "epoch: 1, batch: 0, loss is: [0.04537241], acc is [0.98]\n",
    "epoch: 1, batch: 200, loss is: [0.04753707], acc is [0.98]\n",
    "epoch: 1, batch: 400, loss is: [0.02443215], acc is [0.99]\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "PaddlePaddle 1.6.0 (Python 3.5)",
   "language": "python",
   "name": "py35-paddle1.2.0"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
